<?php

/**
 * @name BankController
 * @desc 控制器
 */
class BankController extends UserCommonController
{
    //公告列表
    public function listAction(){

        $params = $this->post();

        $row = $params['pageSize'] ?? 12;
        $page = $params['page'] ?? 1;

        $userBankModel = UserbankModel::getSingleton();
        $list = $userBankModel->db
            ->where(['user_id'=> $this->user_info['user_id'], 'is_delete'=> 0])
            ->order('id asc')
            ->limit(($page - 1) * $row, $row)
            ->select();
        $total = $userBankModel->db
            ->where(['user_id'=> $this->user_info['user_id'], 'is_delete'=> 0])
            ->count();

        foreach($list as &$val){
            $val['bank_no'] = \Vendor\Kstring::hidebankno($val['bank_no']);
        }

        $data = [
            'list' => $list,
            'total' => intval($total),
            'current'   => intval($page),
            'pageSize'  => intval($row),
            'page'  => intval($page),
            'pageNum'   => ceil($total/$row)
        ];

        $this->ajaxReturn(0, '', $data);
    }

    //详情
    public function detailAction(){
        $params = $this->post();

        if(!isset($params['id'])){
            $this->ajaxReturn(400, '未获取到数据ID');
        }

        $userBankModel = UserbankModel::getSingleton();
        $detail = $userBankModel->find(['id'=> $params['id'], 'user_id'=> $this->user_info['user_id']]);

        if(!$detail){
            $this->ajaxReturn(400, '数据不存在');
        }

        $this->ajaxReturn(0, 'success', $detail);
    }

    //添加银行卡
    public function addAction(){
        $params = $this->post();

        $rule = [
            'bank_id'     => 'require',
            'bank_name' => 'require',
            'account_name' => 'require',
            'bank_no' => 'require|length:16,19',
            'bank_address'  => 'require',
            'bank_branch_name' => 'require',
        ];
        $msg = [
            'bank_id.require' => '必须选择开户行',
            'bank_name.require'   => '必须选择开户行.',
            'account_name.require' => '必须填写开户名',
            'bank_no.require'   => '必须填写银行卡号',
            'bank_no.length'   => '银行卡号不正确',
            'bank_address.require'   => '请选择地区',
            'bank_branch_name.require'   => '必须填写支行名称',
        ];
        $validate = new \Vendor\Kvalidate($rule,$msg);
        if(!$validate->check($params)){
            $this->ajaxReturn(400, $validate->getError());
        }

        $params['user_id'] = $this->user_info['user_id'];
        $userBankModel = UserbankModel::getSingleton();

        $exist = $userBankModel->find(['user_id'=> $this->user_info['user_id'], 'bank_no'=> $params['bank_no'], 'is_delete'=> 0]);
        if($exist){
            $this->ajaxReturn(400, '银行卡已存在');
        }

        $id = $userBankModel->add($params);
        if(!$id){
            $this->ajaxReturn(400, '添加失败');
        }

        //检查第一张默认
        $defaultList = $userBankModel->find(['user_id'=> $this->user_info['user_id'], 'is_default'=> 1]);
        if(!$defaultList){
            $userBankModel->update(['id'=> $id], ['is_default'=> 1]);
        }

        $this->ajaxReturn(0, '添加成功');
    }

    public function setdefaultAction(){
        $params = $this->post();
        if(!isset($params['id']) || !$params['id']){
            $this->ajaxReturn(400, '必须提交数据ID');
        }

        $userBankModel = UserbankModel::getSingleton();
        if(!$userBankModel->update(['user_id'=> $this->user_info['user_id']], ['is_default'=> 0, 'update_time'=> time()])){
            $this->ajaxReturn(400, '设置失败');
        }
        if(!$userBankModel->update(['user_id'=> $this->user_info['user_id'],'id'=> $params['id']], ['is_default'=> 1, 'update_time'=> time()])){
            $this->ajaxReturn(400, '设置失败.');
        }

        $this->ajaxReturn(0, '设置成功');
    }

    //删除
    public function delAction(){
        $params = $this->post();
        if(!isset($params['id']) || !$params['id']){
            $this->ajaxReturn(400, '未获取到ID');
        }

        $userBankModel = UserbankModel::getSingleton();
        if(!$userBankModel->update(['user_id'=> $this->user_info['user_id'], 'id'=> $params['id']], ['is_delete'=> 1, 'update_time'=> time()])){
            $this->ajaxReturn(400, '操作失败');
        }
        $this->ajaxReturn(0, 'success');
    }

    //银行列表
    public function blistAction(){
        $bankModel = BankModel::getSingleton();
        $list = $bankModel->findAll(['is_show'=> 1]);
        $this->ajaxReturn(0, '', $list);
    }

    //获取地址
    public function alistAction(){
        $params = $this->post();
        if(!isset($params['id'])){
            $this->ajaxReturn(400, '参数错误');
        }
        $areasModel = AreasModel::getSingleton();
        $list = $areasModel->findAll(['parent_id'=> $params['id']]);

        $this->ajaxReturn(0, 'success', $list);
    }

}
